<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="item in PatinentList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="deletePatinet(item.id)"
        >
          <view
            class="archive-card"
            :class="{ active: patinentId === item.id }"
            @click="selectPatinet(item.id)"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <!-- {{ item.idCard.replace(/^(.{6}).+(.{4})$/,'$1********$2') }} -->
              <text class="id-card">
                {{ item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2') }}</text
              >
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="PatinentList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-view>
</template>

<script lang="ts" setup>
  import { patientListApi, removePatientApi } from '@/apis/patient'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/types/patient'
  import { onLoad, onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'
  //获取路径参数
  const props = defineProps<{ type?: string }>()
  if (props.type) {
    console.log('咨询')
  } else {
    console.log('我的页面进入的')
  }
const patinentId = ref('')
   const store = useConsultStore()
  const selectPatinet = (id: string) => {
    if (!props.type) {
      return
    }
    patinentId.value = id
    store.setPatinentId(id)
    uni.navigateTo({url:'/subpkg_consult/paymnet/index'})
  }
  // Patinent
  const PatinentList = ref<PatientItem[]>([])
  const getPatientList = async () => {
    const res = await patientListApi()
    console.log(res)
    PatinentList.value = res
  }
  onShow(() => {
    getPatientList()
  })
  //  onLoad(() => {
  //   getList()
  // })
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])
  const deletePatinet = async (id: string) => {
    const res = await uni.showModal({
      title: '删除',
      content: '真的要删除吗',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
    })
    if (res.confirm) {
      await removePatientApi(id)
      if (store.patinentId === id) store.patinentId = ''
      await getPatientList()
      uni.showToast({
        title: '删除成功',
        icon: 'none',
      })
    }
  }
</script>

<style lang="scss">
  @import './index.scss';
</style>
